package com.yksj.healthtalk.comm;

import org.universalimageloader.core.ImageLoader;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.open.share.OpenManager;
import com.open.share.interfaces.IOpenResponse;
import com.open.share.net.OpenResponse;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ShareUtils;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 
 * 分享dialog
 * @author zhao
 *
 */
@SuppressLint("ValidFragment")
public class ShareDialogFragment extends DialogFragment{
	ImageView mImageView;
	String mPath;
	EditText mEditText;
	
	public interface OnShareResultListener {
		void onResult(int result);
	}
	OnShareResultListener mListener;
	private LodingFragmentDialog showLodingDialog;
	
	public ShareDialogFragment(OnShareResultListener listener) {
		this.mListener = listener;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Dialog view = new Dialog(getActivity(),R.style.translucent_dialog);
		view.setContentView(R.layout.dialog_share_layout);
		int type = getArguments().getInt(OpenManager.BUNDLE_KEY_TYPE);
		mPath = getArguments().getString(OpenManager.BUNDLE_KEY_IMGPATH);
		String text = getArguments().getString(OpenManager.BUNDLE_KEY_TEXT);
		final TextView numberTextV = (TextView)view.findViewById(R.id.number_txt);
		
		mEditText = (EditText)view.findViewById(R.id.content);
		mEditText.setText(text);
		TextView textView = (TextView)view.findViewById(R.id.dialog_title);
		switch(type){
		case OpenManager.TENCENT_WEIBO:
			textView.setText("腾讯微博");
			break;
		case OpenManager.SINA_WEIBO:
			textView.setText("新浪微博");
			break;
		case OpenManager.RENREN:
			textView.setText("人人分享");
			break;
		}
		mImageView = (ImageView)view.findViewById(R.id.pic);
		view.findViewById(R.id.dialog_cancle).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ShareDialogFragment.this.dismissAllowingStateLoss();
			}
		});
		view.findViewById(R.id.dialog_true).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onShare();
			}
		});
		
		mEditText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			@Override
			public void afterTextChanged(Editable s) {
				numberTextV.setText(s.length()+"/140");
			}
		});
		numberTextV.setText(mEditText.getText().length()+"/140");
		return view;
	}	
	
	@Override
	public void onActivityCreated(Bundle arg0) {
		super.onActivityCreated(arg0);
		if(mPath != null){
			ImageLoader.getInstance().displayImage(mPath,mImageView);
		}
	}
	
	private void onShare(){
		showLodingDialog = LodingFragmentDialog.showLodingDialog(getFragmentManager(), getResources());
		Bundle bundle = getArguments();
		bundle.putString(OpenManager.BUNDLE_KEY_TEXT,mEditText.getText().toString());
		ShareUtils.share(bundle.getInt(OpenManager.BUNDLE_KEY_TYPE),bundle,getActivity(),new IOpenResponse() {
			@Override
			public boolean response(int arg0, final Object arg1) {
				getActivity().runOnUiThread(new Runnable() {
					@Override
					public void run() {
						showLodingDialog.dismissAllowingStateLoss();
						ShareDialogFragment.this.dismissAllowingStateLoss();
						OpenResponse response=(OpenResponse) arg1;
						if(response.ret==0){//分享成功
							ToastUtil.showBasicShortToast(getActivity(),"分享成功");
						}else if(response.ret == 20019){
							ToastUtil.showBasicShortToast(getActivity(),"你已分享过");
						}else{
							ToastUtil.showBasicShortToast(getActivity(),"分享失败");
						}
						if(mListener != null)mListener.onResult(response.ret);
					}
				});
				return true;
			}
		});
	}
	
	/**
	 * 
	 * @param listener 监听
	 * @param fg 
	 * @param type 类型  文本OpenManager.BUNDLE_KEY_TEXT
	 * @param cotnent 内容
	 * @param path 图片路径
	 */
	public static void show(FragmentManager fg,int type,String cotnent,String path,OnShareResultListener listener){
		ShareDialogFragment fragment = new ShareDialogFragment(listener);
		Bundle bundle = new Bundle();
		bundle.putString(OpenManager.BUNDLE_KEY_TEXT,cotnent);
		if(path != null)bundle.putString(OpenManager.BUNDLE_KEY_IMGPATH,path);
		bundle.putInt(OpenManager.BUNDLE_KEY_TYPE,type);
		fragment.setArguments(bundle);
		FragmentTransaction ft = fg.beginTransaction();
		ft.add(fragment, "SHARE");
		ft.commitAllowingStateLoss();
	}
}
